﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConfigurationManager.Model;
using System.Diagnostics;
using PSE.Framework.Configuration;
using System.IO;

namespace ConfigurationManager.View
{
    public partial class frmExport : Form
    {
        public frmExport()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmExport_Load);
        }

        private void frmExport_Load(object sender, EventArgs e)
        {
            SystemComponent s;

            cboProduct.DisplayMember = "Name";
            cboProduct.ValueMember = "Id";
            cboProduct.DataSource = Model.Product.Retrieve().Where(p => SystemComponents.TryGetSystem(p.Name, out s)).ToList();

            //Model.Exporter.Export(new ConfigurationManager.Model.ProductExportInfo()
            //{
            //    ProductName = "IMercado",
            //    ProcessesFolderPath = @"C:\DSEPROJ\IMercado\Compilados\Servicos\",
            //    PSEProcessManagerInstallPath = @"C:\DSEPROJ\PSE\Compilados\ProcessManager\",
            //    ServiceCertificateName = "IMercadoService",
            //    STSAuthorityCertificateName = "IMercadoSTSAuthority"
            //});

            //Model.Exporter.Export(new ConfigurationManager.Model.ProductExportInfo()
            //{
            //    ProductName = "Sinacor",
            //    ProcessesFolderPath = @"C:\Sinacor\Mais\Projetos\Compilados\Processos\",
            //    PSEProcessManagerInstallPath = @"C:\DSEPROJ\PSE\Compilados\ProcessManager\",
            //    ServiceCertificateName = "SinacorService",
            //    STSAuthorityCertificateName = "SinacorSTSAuthority"
            //});
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (cboProduct.SelectedIndex < 0)
                MessageBox.Show("Selecione um produto.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            ProductExportInfo pi = new ProductExportInfo();
            pi.ProductName = ((Model.Product)cboProduct.SelectedItem).Name;
            pi.ProcessesFolderPath = txtProcessesPath.Text;
            pi.ServiceCertificateName = txtServiceCertificate.Text;
            pi.STSAuthorityCertificateName = txtStsCertificate.Text;

            try
            {
                Model.Exporter.Export(pi);
                MessageBox.Show("Exportação realizada com sucesso!", "Process Manager Configuration", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                //EventLog.WriteEntry("Process Manager - Configuration Manager", ex.ToString(), EventLogEntryType.Error);
                //MessageBox.Show("Ocorreu um erro interno, verifique o event viewer para maiores detalhes");
                Program.HandleException(ex);
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() != DialogResult.Cancel)
                txtProcessesPath.Text = dialog.SelectedPath;
        }

        private void cboProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboProduct.SelectedIndex >= 0)
            {
                string pName = ((Model.Product)cboProduct.SelectedItem).Name;
                               
                txtProcessesPath.Text = Path.Combine(ApplicationConfiguration.GetApplicationRootDirectory(SystemComponents.GetSystem(pName)), "Processos");
                txtStsCertificate.Text = string.Format("{0}STSAuthority", pName);
                txtServiceCertificate.Text = string.Format("{0}Service", pName);
            }
        }
    }
}
